/**
 * 
 */
package com.sip.comadapter.executor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ReleaseVersion;
import com.sip.dbadapter.dao.domain.SystemInfo;
import com.sip.dbadapter.dao.exception.CustomException;
import com.sip.dbadapter.dao.interfaces.SystemInfoDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;
import com.sip.dbadapter.domain.registry.DomainRegistry;
import com.sip.ruleevaluateadapter.EvaluationManager;
import com.sip.scanadapter.factory.ScanStrategyFactory;
import com.sip.scanadapter.strategy.Stratergy;

/**
 * @author Dhanushka Ariyasingha.
 */
public class WADLScanExecutor extends BaseExecutor implements Executor {

    // get log4j handler
    private static final Logger logger = Logger.getLogger(WADLScanExecutor.class);

    public CommunicationMessage execute(final CommunicationMessage message) throws Exception {

        // TODO Auto-generated method stub

        logger.info("<<<<<<<<<<<<<<< GenericAPIFilteringExecutor >>>>>>>>>>>>>>>");

        final Object obj = message.getObject();

        final Map<String, Object> domainCollection = (Map<String, Object>) obj;

        final ReleaseVersion releaseVersion = (ReleaseVersion) domainCollection.get(DomainRegistry.RELEASE_VERSION);
        final SystemInfo systemInfo = (SystemInfo) domainCollection.get(DomainRegistry.SYSTEM_INFO);

        logger.info(">>>>>>>>>>>>RELEASE ID:>>>>>>>>>>>" + releaseVersion.getReleaseId());
        logger.info(">>>>>>>>>>>>URL:>>>>>>>>>>>" + releaseVersion.getUrl());
        logger.info(">>>>>>>>>>>>>SYSTEM NAME:>>>>>>>>>>" + systemInfo.getSystemName());

        final Stratergy strategy = ScanStrategyFactory.getInstance().getStrategy(Stratergy.STRATEGY_WADL);

        final Map<String, Object> resourceMap = new HashMap<String, Object>();

        resourceMap.put(Stratergy.END_POINT, releaseVersion.getUrl());
        final Set<ApiDefinition> apiDefinitionSet = strategy.build(resourceMap);

        SystemInfoDao systemInfoDao = (SystemInfoDao) DaoLoadFactory.getDao(DaoNameConstant.SYSTEM_DAO);
        SystemInfo existingSystemInfoObject = checkSystemNameAvaiability(systemInfoDao, systemInfo);
        systemInfo.setApiDefinitionSet(apiDefinitionSet);

        if (null != existingSystemInfoObject) {

            logger.info("==========================Save Update Call=================================");
            EvaluationManager manager = new EvaluationManager();
            manager.evaluateSystemUpdates(systemInfo);
        } else {
            logger.info("==========================Insert Call=================================");
            systemInfoDao.save(systemInfo);

        }

        message.setMessageBody("NO_OF_API_SCAN:|NO_OF_PRODUCER_TYPE:|NO_OF_CONSUMER_TYPE:");

        return message;
    }

    private SystemInfo checkSystemNameAvaiability(SystemInfoDao systemInfoDao, SystemInfo scanedSytemInfoObject) throws CustomException {

        List<Object[]> systemInfoList = new ArrayList<Object[]>();

        systemInfoList = (List<Object[]>) systemInfoDao.getSystemData(new String[] {"id", "systemName",});

        String scanSystemName = scanedSytemInfoObject.getSystemName();
        boolean nameFound = false;
        Long systemId = null;
        for (Object[] object : systemInfoList) {
            logger.info("==system Info==>" + object[0].toString() + "___" + object[1].toString());
            if (scanSystemName.equals(object[1].toString())) {
                systemId = Long.valueOf(object[0].toString());
                nameFound = true;
                break;
            }
        }
        logger.info("status========>" + nameFound);

        SystemInfo systemInfo = null;
        if (nameFound) {
            systemInfo = systemInfoDao.findById(systemId);
        }

        return systemInfo;

    }

}
